package cn.vc.content.controller.article;

import cn.vc.auth.entity.po.VUser;
import cn.vc.basicmodule.entity.Result;
import cn.vc.content.entity.article.ArticleReview;
import cn.vc.content.entity.video.VideoReview;
import cn.vc.content.service.article.ArticleReviewService;
import cn.vc.content.service.article.ArticleService;
import cn.vc.content.service.video.VideoReviewService;
import cn.vc.content.service.video.VideoService;
import cn.vc.content.util.SecurityUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评论及回复
 */
@RestController
@RequestMapping("/articlereview")
public class ArticleReviewController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private ArticleReviewService articleReviewService;
    @Resource
    private ArticleService articleService;


    /**
     * 评论
     * @param articleId
     * @param content
     * @return
     */
    @PostMapping("/1")
//    @PreAuthorize("hasAnyAuthority('ordinary')")
    public Result<ArticleReview> review(@RequestParam("articleId") Long articleId, @RequestParam("content") String content) {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();

        ArticleReview review = new ArticleReview();
        review.setArticleIds(articleId);
        review.setUserId(myUser.getId());
        review.setUsername(myUser.getUsername());
        review.setArticleId(articleId);
        review.setContent(content);
        boolean bo = articleReviewService.review(review);
        if (bo) {
            // 评论数加一
            articleService.review(articleId);
            return new Result<>(1, review, null);
        } else {
            return new Result<>(0);
        }

    }
    /**
     * 回复
     * @param articleIds
     * @param reviewId
     * @param content
     * @return
     */
    @PostMapping("/2")
//    @PreAuthorize("hasAnyAuthority('ordinary')")
    public Result<ArticleReview> recover(@RequestParam("articleIds") Long articleIds, @RequestParam("reviewId") Long reviewId, @RequestParam("content") String content) {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();

        ArticleReview recover = new ArticleReview();
        recover.setArticleIds(articleIds);
        recover.setUserId(myUser.getId());
        recover.setUsername(myUser.getUsername());
        recover.setReviewId(reviewId);
        recover.setContent(content);
        boolean bo = articleReviewService.review(recover);
        if (bo) {
            // 评论数加一
            articleService.review(articleIds);
            return new Result<>(1, recover, null);
        } else {
            return new Result<>(0);
        }
    }


    /**
     * 查询视频评论
     * @param videoId
     * @return
     */
    @GetMapping("/1/{articleId}")
    public Result<List<ArticleReview>> selectReview(@PathVariable("articleId") Long videoId) {
        List<ArticleReview> articleReviewList = articleReviewService.selectReview(videoId);
        return new Result<>(1, articleReviewList);
    }

}
